package dao.impl;


import dao.UserDao;
import pojo.User;
import util.DataUtils;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;

public class UserDaoImpl implements UserDao {
    private Connection conn = null;
    private ResultSet rs = null;
    private boolean flag = false;

    public boolean login(String usename, String password) throws SQLException {
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select * from yonghu where usename='" + usename + "' and password='" + password + "'");
        while (rs.next()) {
            if (rs.getString("usename").equals(usename) && rs.getString("password").equals(password)) {
                flag = true;
                break;
            }
        }
        JdbcUtils.closeconn(rs, null, conn);
        return flag;
    }


    public boolean update(User user) throws SQLException, IllegalAccessException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = "update yonghu set usename='" + user.getUsename() + "',password='" + user.getPassword() + "',gender='" + user.getGender() + "',age='" + user.getAge() + "',telephone='" + user.getTelephone() + "' where uid='" + user.getUid() + "'";
        //String sql= String.valueOf(SqlImpl.updateSql(user));
        i = JdbcUtils.addsql(sql);
        if (i > 0) flag = true;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }


    public boolean addRegist(User user) throws SQLException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = "insert into yonghu values(null,'" + user.getUsename() + "','" + user.getPassword() + "','" + user.getAge() + "','" + user.getTelephone() + "','" + user.getGender() + "','" + 0 + "','" + 0 + "')";
        i = JdbcUtils.addsql(sql);
        if (i > 0) flag = true;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }


    public User getUser(Integer uid) throws SQLException, ClassNotFoundException {
        User user = new User();
        conn = JdbcUtils.dbconn();
        String sql = "select * from yonghu where uid='" + uid + "'";
        rs = JdbcUtils.selectsql(sql);
        user = (User) DataUtils.dataEncapsulation(Class.forName("pojo.User"), rs);
        JdbcUtils.closeconn(rs, null, conn);
        return user;
    }


    public String findManagerUsername(Integer mid) throws SQLException {
        String usename = null;
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select usename from yonghu where mid='" + mid + "'");
        while (true) {
            if (!rs.next()) break;
            usename = rs.getString("usename");
        }
        JdbcUtils.closeconn(rs, null, conn);
        return usename;
    }


    public Integer findManagerMid(String type) throws SQLException {
        Integer mid = 0;
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select mid from manager where type='" + type + "'");
        while (true) {
            if (!rs.next()) break;
            mid = rs.getInt("mid");
        }
        JdbcUtils.closeconn(rs, null, conn);
        return mid;
    }


    public HashSet<String> areaUserUsename(String type) throws SQLException {
        Integer mid = 0;
        UserDao userDao = new UserDaoImpl();
        mid = userDao.findManagerMid(type);
        String usename = userDao.findManagerUsername(mid);
        HashSet<String> usenameList = new HashSet<>();
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select usename from biji where type='" + type + "'");
        while (true) {
            if (!rs.next()) break;
            if (!rs.getString("usename").equals(usename)) {
                usenameList.add(rs.getString("usename"));
            }
        }
        JdbcUtils.closeconn(rs, null, conn);
        return usenameList;
    }


    public boolean updatePower(User user) throws SQLException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = null;
        if (user.getAddpower() == 1) {
            sql = "update yonghu set addpower='" + 0 + "'  where uid='" + user.getUid() + "'";
        } else if (user.getAddpower() == 0) {
            sql = "update yonghu set addpower='" + 1 + "'  where uid='" + user.getUid() + "'";
        }
        i = JdbcUtils.addsql(sql);
        if (i > 0) flag = true;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }


    public boolean findPower(Integer uid) throws SQLException {
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select addpower from yonghu where uid='" + uid + "'");
        while (true) {
            if (!rs.next()) {
                break;
            }
            while (rs.next()) {
                if (rs.getInt("addpower") == 1) {
                    flag = true;
                    break;
                }
            }
        }
        JdbcUtils.closeconn(rs, null, conn);
        return flag;
    }


    public boolean findPowerByUsename(String usename) throws SQLException {
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select addpower from yonghu where usename='" + usename + "'");
        while (true) {
            if (!rs.next()) {
                break;
            }
            if (rs.getInt("addpower") == 0) {
                flag = true;
                break;
            }
        }
        JdbcUtils.closeconn(rs, null, conn);
        return flag;
    }

    public User findUserInformation(String usename) throws SQLException, ClassNotFoundException {
        User user = new User();
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select * from yonghu where usename='" + usename + "'");
        user = (User) DataUtils.dataEncapsulation(Class.forName("pojo.User"), rs);
        JdbcUtils.closeconn(rs, null, conn);
        return user;
    }


    public String findAreaManager(Integer mid) throws SQLException {
        String type = null;
        User user = new User();
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select type from manager where mid='" + mid + "'");
        while (true) {
            if (!rs.next()) break;
            type = rs.getString("type");
        }
        JdbcUtils.closeconn(rs, null, conn);
        return type;
    }


    public ArrayList<User> allUsers() throws SQLException, ClassNotFoundException {
        ArrayList<User> allUsers = new ArrayList<>();
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select * from yonghu");
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            User user = new User();
            user = (User) DataUtils.dataEncapsulation(Class.forName("pojo.User"), rs);
            allUsers.add(user);
        } while (row > 0);
        JdbcUtils.closeconn(rs, null, conn);
        return allUsers;
    }


}